﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Runtime;

namespace PServiceBus.Core.Interface {
    /// <summary>
    /// Handler for reading from data store/queue and translating message from the source to specific [TMessage]
    /// </summary>
    /// <typeparam name="TMessage">The type of the message.</typeparam>
    public interface IMessageHandler<TMessage> where TMessage : class {
        /// <summary>
        /// Initialization method that is called before starting handler
        /// </summary>
        void Init();
        /// <summary>
        /// Occurs when message received.
        /// </summary>
        event EventHandler<MessageReceivedArgs<TMessage>> MessageReceived;
        /// <summary>
        /// Occurs when exception is thrown
        /// </summary>
        event EventHandler<HandleErrorArgs<TMessage>> ErrorReceived;
        /// <summary>
        /// Method for calling MessageReceived event
        /// </summary>
        /// <param name="e"></param>
        void OnMessageReceived(MessageReceivedArgs<TMessage> e);
        /// <summary>
        /// Method for calling ErrorReceived event
        /// </summary>
        /// <param name="e"></param>
        void OnErrorReceived(HandleErrorArgs<TMessage> e);
        /// <summary>
        /// Gets a value indicating whether the handler is running.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if handler is running; otherwise, <c>false</c>.
        /// </value>
        bool IsRunning { get; }
        /// <summary>
        /// Execute message handling in listener thread if true
        /// </summary>
        bool HandleInListenerThread { get; set; }
        /// <summary>
        /// Numbers of item to retrieve for each interval
        /// </summary>
        int BatchSize { get; set; }
        /// <summary>
        /// Set the underlying data store endpoint
        /// </summary>
        string Endpoint { set; }
        /// <summary>
        /// How often to poll for new message
        /// </summary>
        TimeSpan Interval { get; set; }
        /// <summary>
        /// Starts listening for new messages
        /// </summary>
        void Start();
        /// <summary>
        /// Stops listening for messages
        /// </summary>
        void Stop();
    }
}
